'''
Created on 2022/06/01

@author: zhangzhe
'''
import sys, os, re

tab_space = "    "


if __name__ == '__main__':
    retry = 0
    while True:
        EN_file = input('Please input en_file path = ').strip()
        JP_file = input('Please input jp_file path = ').strip()
        # input_path = './header'
        if retry > 3:
            print(f'error exit.')
            sys.exit(1)
        elif not os.path.exists(EN_file):
            retry += 1
            print(f'{EN_file} is not exits.')
            continue
        else:
            break

    try:
        checklog = open("./check_vehicle_settings.log", mode = 'w', encoding = 'utf-8')

        pattern_line = re.compile(r'.*\n')

        file_line = 0
        en_lines = []
        en_lines_num = 0
        with open(EN_file, mode='r', encoding='utf-8', errors='ignore', newline='\n') as fh_r:
            file_content = fh_r.read() + '\n'
            # linesカウント
            for m in re.finditer(pattern_line, file_content):
                file_line += 1
                if file_line > 53:
                    en_lines.append(m.group())

        file_line = 0
        jp_lines = []
        jp_lines_num = 0
        with open(JP_file, mode='r', encoding='utf-8', errors='ignore', newline='\n') as fh_r:
            file_content1 = fh_r.read() + '\n'
            # linesカウント
            for m in re.finditer(pattern_line, file_content1):
                file_line += 1
                if file_line > 53:
                    jp_lines.append(m.group())

        checklog.write(f'{tab_space}=======================================================\n')
        checklog.write(f'{tab_space}「EN_file」と「JP_file」を確認する.\n')
        checklog.write(f'{tab_space}=======================================================\n')

        table_pattern = re.compile('^\| \d+\s+\|(.*)\|(.*)\|(.*)\|$', re.I)
        en_line_dict = []
        for line in en_lines:
            match_obj = re.search(table_pattern, line)
            if match_obj:
                en_lines_num += 1
                en_line_dict.append(match_obj.group(3).strip())

        jp_line_dict = []
        for line in jp_lines:
            match_obj = re.search(table_pattern, line)
            if match_obj:
                jp_lines_num += 1
                jp_line_dict.append(match_obj.group(3).strip())

        if en_lines_num == en_lines_num:
            for num in range(len(en_line_dict)):
                if en_line_dict[num] != jp_line_dict[num]:
                    checklog.write(f'{tab_space}[{num+1}] : EN=[{en_line_dict[num]}]=====>>>>>JP=[{jp_line_dict[num]}]\n')
        else:
            checklog.write(f'{tab_space}一貫性のない量のファイルデータ\n')



    except OSError as err:
        print("OS error: {0}".format(err))
    except ValueError:
        print("Could not convert data to an integer.")
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise